import User from "../../model/user"
import { getDataSet, getEventParam } from "../../utils/util";
import cache from "../../enum/cache";
import APIConfig from "../../config/api"
import Information from "../../model/information";

Page({
  data: {
    userInfo: {
      nickname: '点击授权登录',
      subname: '',
      avatar: '../../assets/images/home-robot.png'
    },
    role: 'expert'
  },

  /**
   * 生命周期函数--监听页面加载
   */
 async onLoad(options) {
    this.init()
  },
 async init() {
  let role = wx.getStorageSync(cache.USER_ROLES)
  if(role === 'expert') {
    let data = wx.getStorageSync(cache.USER_INFO)
    this.setData({
      [`userInfo.nickname`]: data.name, 
      [`userInfo.avatar`]: `${APIConfig.baseUrl}/minio/minioFileDownload?fileName=${data.avatar}&bucketName=touxiang`,
      [`userInfo.subname`]: data.level
    })
  } else if(role === 'common') {
    let data = wx.getStorageSync(cache.COMMON_INFO)
    this.setData({
      [`userInfo.nickname`]: data.nickName, 
      [`userInfo.subname`]: data.dept.deptName
    })
  }
 },
  /**
   * 生命周期函数--监听页面显示
   */
 onShow() {
  this.init()
  },

  // 授权登录
  handleToLogin() {
    wx.navigateTo({
      url: '/pages/login/index',
    })
  },
  // 登出
  async handleToLogout() {
    wx.showToast({
      title: '退出登录',
      duration: 2000
    })
    let res = await User.logout()
    // wx.navigateTo({
    //   url: '/pages/login/index',
    // })
    if(res.code == 200) {
      this.setData({
        [`userInfo.nickname`]: '点击授权登录',
        [`userInfo.avatar`]: '',
        [`userInfo.subname`]: ''
      })
    }
  }
})